package com.study.java.web.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.study.java.utils.DateConverterUtils;

/**
* @Name: UserFormBean
* @Description: 表单VO类：用户表单信息类
* @Author: XXX
* @CreateDate: XXX
* @Version: V1.0
 */
public class UserFormBean {
	
	private String username ;
	private String password ;
	private String repassword ;
	private String birthday ;
	private String email ;
	//校验错误信息集合
	private Map<String, String> errorMsg = new HashMap<String, String>() ;
	
	public UserFormBean() {}
	
	public UserFormBean(String username, String password, String repassword,
			String birthday, String email) {
		this.username = username;
		this.password = password;
		this.repassword = repassword;
		this.birthday = birthday;
		this.email = email;
	}
	
	/**
	* @Name: validate
	* @Description: 校验注册字段格式
	* @Author: XXX
	* @Version: V1.0
	* @CreateDate: XXX
	* @Parameters: 
	* @Return: void
	 */
	public boolean validate() {
		
		if(username == null || "".equals(username.toString().trim())) {
			errorMsg.put("username", "用户名不能为空！") ;
		} else {
			if(!username.matches("^[a-zA-Z]{3,8}$")) {
				errorMsg.put("username", "用户名必须为3-8位的字母！") ;
			}
		}
		
		if(password == null || "".equals(password)) {
			errorMsg.put("password", "密码不能为空！") ;
		} else {
			if(!password.matches("^[a-zA-Z]{3,8}$")) {
				errorMsg.put("password", "密码必须为3-8位的字母！") ;
			}
		}
		
		if(!repassword.equals(password)) {
			errorMsg.put("repassword", "与原密码输入不一致！") ;
		}
		
		if(birthday == null || "".equals(birthday)) {
			errorMsg.put("birthday", "出生年月不能为空！") ;
		} else {
			try {
				DateConverterUtils.parse(birthday) ;
			} catch (Exception e) {
				errorMsg.put("birthday", "输入格式(yyyy-MM-dd)不正确！") ;
			}
		}
		if(email == null || "".equals(email)) {
			errorMsg.put("email", "邮箱不能为空！") ;
		} else {
			if(!email.matches("\\b^['_a-z0-9-\\+]+(\\.['_a-z0-9-\\+]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2}|aero|arpa|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|nato|net|org|pro|tel|travel|xxx)$\\b")) {
				errorMsg.put("email", "邮箱格式错误！") ;
			}
		}
		
		return errorMsg.isEmpty() ;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public Map<String, String> getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(Map<String, String> errorMsg) {
		this.errorMsg = errorMsg;
	}

	@Override
	public String toString() {
		return "UserFormBean [username=" + username + ", password=" + password
				+ ", repassword=" + repassword + ", birthday=" + birthday
				+ ", email=" + email + "]";
	}
	
}
